
import os
import cv2
import dlib
import glob
import sys
# 测试部分
# 导入训练好的模型文件
current_path = os.getcwd()#获取当前工作路径

faces_path = 'F:/dlib-master/tools/imglab/build/Release/images_6000'

predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
detector = dlib.get_frontal_face_detector()

savedStdout = sys.stdout  #保存标准输出流
print_log = open("printlogImagine6000.txt","w")
sys.stdout = print_log
print("<images>")
for f in glob.glob(os.path.join(faces_path, "*.png")):
    img = cv2.imread(f)
    img2 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    dets = detector(img2, 1)
    if(len(dets)==0):
        continue
    for index, face in enumerate(dets):
        left = face.left()
        top = face.top()
        right = face.right()
        bottom = face.bottom()
        print(" <image file='"+f+"' width='640' height='480'>")
        print("   <box top='"+str(top)+"' left='"+str(left)+"' width='"+str(right-left)+"' height='"+str(bottom-top)+"'>")
        # print('face {}; left {}; top {}; right {}; bottom {}'.format(index, face.left(), face.top(), face.right(), face.bottom()))
        shape = predictor(img, face)
        for index, pt in enumerate(shape.parts()):
            if(index==36 or index==37 or index==38 or index==39 or index==40 or index==41 or index==42 or index==43 or index==44 or index==45 or index==46 or index==47 or index==48 or index==50 or index==52 or index==56 or index==58 or index==54):
                print("     <part name='"+str(index)+"' x='"+str(pt.x)+"' y='"+str(pt.y)+"'/>")
        print("  </box>")
    print(" </image>")
sys.stdout = savedStdout  #恢复标准输出流
print ('This message is for screen!')
print_log.close()
cv2.waitKey(0)
cv2.destroyAllWindows()
